/*
package com.gitee.yannzh.rune.core.excel.converter;

import cn.hutool.core.lang.Dict;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ConverterKeyBuild;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;

*/
/**
  * @description: excel字典转换器
  * @author yann
  * @date 2022/2/24 18:17
  * @version：1.0
  *//*

public class DictConverter implements Converter<Object> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return (Class<?>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }


    */
/**
     * Convert excel objects to Java objects
     *
     * @param context read converter context
     * @return Data to put into a Java object
     * @throws Exception Exception.
     *//*

    @Override
    public Object convertToJavaData(ReadConverterContext<?> context) throws Exception {
        ReadCellData<?> cellData = context.getReadCellData();
        ExcelContentProperty contentProperty = context.getContentProperty();
        AnalysisContext analysisContext = context.getAnalysisContext();
        Field field = contentProperty.getField();
        Class<?> clazz = field.getType();
        Converter<?> converter = analysisContext.readSheetHolder().converterMap().get(ConverterKeyBuild.buildKey(clazz, cellData.getType()));
        Dict dict = field.getAnnotation(Dict.class);
        if (dict != null) {
            IDictConvertHandler handler = this.getDictConvertHandler();
            if (handler == null){
                throw new SystemException(String.format("%s接口未实现",IDictConvertHandler.class));
            }
            String key = handler.getKey(dict.code(), cellData.getStringValue());
            context.getReadCellData().setStringValue( StringUtils.isBlank(key)?  cellData.getStringValue() : key);
        }
        return converter.convertToJavaData(context);
    }

    @Override
    public WriteCellData<?> convertToExcelData(Object value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        Dict dict = contentProperty.getField().getAnnotation(Dict.class);
        if (dict != null) {
            IDictConvertHandler handler = this.getDictConvertHandler();
            if (handler == null){
                throw new SystemException(String.format("%s接口未实现",IDictConvertHandler.class));
            }
            String val = handler.getValue(dict.code(), value.toString());
            value = StringUtils.isBlank(val)? value : val;
        }
        WriteCellData data = new WriteCellData();
        data.setData(value);
        data.setStringValue(value.toString());
        data.setType(supportExcelTypeKey());
        return data;
    }


    private IDictConvertHandler getDictConvertHandler(){
       return SpringUtil.getBean(IDictConvertHandler.class);
    }

}
*/
